package com.yyy.trans.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyy.trans.entity.UserEntity;
import com.yyy.trans.mapper.UserMapper;
import com.yyy.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public int createUserWrong1(String name) {
        try {
            userMapper.insert(new UserEntity(1L,name));
            throw new RuntimeException("error");
        } catch (RuntimeException e) {
            log.error("create user failed", e);
        }
        return 1;
    }

    @Override
    @Transactional
    public int createUserWrong2(String name) throws IOException {
        userMapper.insert(new UserEntity(2L, name));
        otherTask();
        return 1;
    }

    private void otherTask() throws IOException {
        Files.readAllLines(Paths.get("file-that-not-exist"));
    }
}
